package com.ce.common.db.dump;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.ce.common.db.base.DataObject;
import com.ce.common.db.base.Database;
import com.ce.common.db.base.PropertiesUtil;
import com.ce.common.db.dump.DumpCommand;
import com.ce.common.db.dump.mysql.MySQLDumpCommand;

/**
 * 组装语句的测试类
 * @author yangenxiong
 *
 */
public class MySQLDumpCommandT {

	Database db;
	
	DumpCommand dump;
	
	@Before
	public void setUp() {
		db = new Database(PropertiesUtil.JDBC_USERNAME, 
				PropertiesUtil.JDBC_PASSWD, PropertiesUtil.JDBC_DATABASE, PropertiesUtil.JDBC_HOST, "");
		dump = new MySQLDumpCommand(db);
	}
	
	@After
	public void tearDown() {
		db = null;
		dump = null;
	}
	
	@Test
	public void testGetDumpCommand() throws Exception {
		List<DataObject> tableName = new ArrayList<DataObject>();
		String command = dump.getDumpCommand(tableName, "test.sql");
		assertEquals("cmd /c mysqldump -u" + PropertiesUtil.JDBC_USERNAME + 
				" -p" + PropertiesUtil.JDBC_PASSWD + " -h " + 
				PropertiesUtil.JDBC_HOST + " " + PropertiesUtil.JDBC_DATABASE + 
				" > test.sql", command);
		
		tableName.add(new DataObject("org_employee", "TABLE"));
		command = dump.getDumpCommand(tableName, "test.sql");
		assertEquals("cmd /c mysqldump -u" + PropertiesUtil.JDBC_USERNAME + " -p" + 
				PropertiesUtil.JDBC_PASSWD + " -h " + PropertiesUtil.JDBC_HOST + 
				" " + PropertiesUtil.JDBC_DATABASE + " --tables org_employee > test.sql", command);
	
		command = dump.getDumpCommand(tableName, "");
		assertEquals("cmd /c mysqldump -u" + PropertiesUtil.JDBC_USERNAME + 
				" -p" + PropertiesUtil.JDBC_PASSWD + " -h " + PropertiesUtil.JDBC_HOST + 
				" " + PropertiesUtil.JDBC_DATABASE + " --tables org_employee > ", command);
		
		
		
		db.setDatabaseName("");
		command = dump.getDumpCommand(tableName, "");
		assertEquals("cmd /c mysqldump -u" + PropertiesUtil.JDBC_USERNAME + 
				" -p" + PropertiesUtil.JDBC_PASSWD + " -h " + PropertiesUtil.JDBC_HOST + "  --tables org_employee > ", command);
	
		
		db.setCharacterSet("UTF8");
		
		db.setDatabaseName("HRDB2");
		command = dump.getDumpCommand(tableName, "test.sql");
		
		assertEquals("cmd /c mysqldump -u" + PropertiesUtil.JDBC_USERNAME + 
				" -p" + PropertiesUtil.JDBC_PASSWD + " -h " + PropertiesUtil.JDBC_HOST + 
				" HRDB2" + " --tables org_employee --default-character-set=UTF8 > test.sql", command);
	}
}
